package Chuong07;

import java.io.*;

public class Bai01 {
	
	ObjectOutputStream write;
	ObjectInputStream read;
	String path = "D:/Hoc Tap/Hoc Ki VII/Chuyen De Java/OnTap_Code/OnTap/src/Chuong07/File/";
	//String path = "Z:/";
	public static boolean IsNguyenTo(long n){
		boolean resuft = true;
		if(n >= 2){
			for(int i = 2; i < n; i++){
				if(n % i == 0) resuft = false;
			}
		}
		else resuft = false;
		return resuft;
	}
	
	public Bai01(){
		try {
			write = new ObjectOutputStream(new FileOutputStream(path + "sonn.dat"));
			
			for (int i = 0; i < 100; i++){
				int ghi = (int) (Math.random()*100);
				write.writeInt(ghi); 
				System.out.print(ghi + " ");
			}
		} catch (IOException e) {		
			e.printStackTrace();
		}
		
		try {
			write.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try{
			read = new ObjectInputStream(new FileInputStream(path + "sonn.dat"));
			write = new ObjectOutputStream(new FileOutputStream(path + "sont.dat"));
			System.out.println();
			for (int i = 0; i < 100; i++){
				int n = read.readInt();
				if(IsNguyenTo(n)){
					write.writeInt(n); 
					System.out.print(n + " ");
				}
			}
			
		} catch (IOException e) {		
			e.printStackTrace();
		}
		
		try {
			write.close();
			read.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new Bai01();

	}

}
